package com.example.repair.activity.order;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMapException;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.AmapPageType;
import com.amap.api.navi.NaviSetting;
import com.amap.api.navi.enums.AMapNaviRingType;
import com.amap.api.navi.model.NaviLatLng;
import com.example.repair.R;
import com.example.repair.activity.MainActivity;
import com.example.repair.adapter.AllOrderAdapter;
import com.example.repair.base.BaseActivity;
import com.example.repair.base.Presenter.PressenterImpl;
import com.example.repair.base.netWork.Constant;
import com.example.repair.base.netWork.LoginContract;
import com.example.repair.entity.Bean;
import com.example.repair.entity.OrderBean;
import com.example.repair.util.SpUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllorderActivity extends BaseActivity implements LoginContract.IView {


    @BindView(R.id.recy)
    XRecyclerView recy;
    @BindView(R.id.title)
    TextView title;
    private PressenterImpl pressenter;
    private String uid;

    int page = 1;
    private AllOrderAdapter allOrderAdapter;
    private String type;
    private String lng;
    private String lat;

    @Override
    protected int getLayout() {
        return R.layout.activity_allorder;
    }


    @Override
    protected void initView() {

        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");
        NaviSetting.updatePrivacyShow(this, true, true);
        NaviSetting.updatePrivacyAgree(this, true);

        type = getIntent().getStringExtra("type");


        lng = SpUtils.getString(AllorderActivity.this, "lng");
        lat = SpUtils.getString(AllorderActivity.this, "lat");

        switch (type){
            case "1":
                title.setText("全部订单");
                break;
            case "2":
                title.setText("已分配");
                break;
            case "3":
                title.setText("正在维修");
                break;
            case "4":
                title.setText("已支付");
                break;
            case "5":
                title.setText("已完成订单");
                break;
            case "-1":
                title.setText("维修失败");
                break;
        }

        showPopwindow();

    }

    @Override
    protected void initData() {

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recy.setLayoutManager(manager);

        allOrderAdapter = new AllOrderAdapter(this,type);

        recy.setAdapter(allOrderAdapter);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                loadOrder();
            }

            @Override
            public void onLoadMore() {
                loadOrder();
            }
        });


        allOrderAdapter.setdeleteClick(new AllOrderAdapter.OnClick() {
            @Override
            public void item(int posttion,int orderId) {

                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid",uid);
                map.put("orderId",orderId+"");
                pressenter.sendMessage(AllorderActivity.this,Constant.order_under_repair,map, Bean.class);

            }
        });

        allOrderAdapter.setdeleteClick(new AllOrderAdapter.OnClickD() {
            @Override
            public void item(View v,double i,double j) {

                lats=i;
                log=j;

                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        page=1;
        allOrderAdapter.clear();
        loadOrder();
    }

    private void loadOrder() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);


        switch (type){
            case "1":
                title.setText("全部订单");
                break;
            case "2":
                map.put("status", "2");
                title.setText("已分配");
                break;
            case "3":
                map.put("status", "3");
                title.setText("正在维修");
                break;
            case "4":
                map.put("status", "4");
                title.setText("已支付");
                break;
            case "5":
                map.put("status", "5");
                title.setText("已完成订单");
                break;
            case "-1":
                map.put("status", "-1");
                title.setText("维修失败");
                break;
        }



        map.put("page",page+"");
        pressenter.sendMessage(this, Constant.orderlist, map, OrderBean.class);
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof OrderBean) {
            OrderBean orderBean = (OrderBean) data;
            if (orderBean.getCode() == 1) {

                if (page==1){
                    allOrderAdapter.setAllorder(orderBean.getData());
                }else {
                    allOrderAdapter.addAllorder(orderBean.getData());
                }
                page++;
            } else {
                Toast.makeText(this, orderBean.getMessage(), Toast.LENGTH_SHORT).show();
            }

            recy.refreshComplete();
            recy.loadMoreComplete();
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;

            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();

            if (bean.getCode()==1){
                page=1;
                loadOrder();
            }

        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    double lats;
    double log;

    private PopupWindow popWindow;

    private void showPopwindow() {
        View popView = View.inflate(this, R.layout.dialog_amap, null);
        TextView cancle = popView.findViewById(R.id.cancle);
        TextView sure = popView.findViewById(R.id.sure);

        final RadioGroup group=popView.findViewById(R.id.group);

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });


        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Poi start;
                Poi end;
                AmapNaviParams params;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.WALK:

                        //起点
                        start = new Poi(null, new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)), null);
                        //终点
                        end = new Poi(null, new LatLng(lats,log), null);
                        // 组件参数配置
                        params = new AmapNaviParams(start, null, end, AmapNaviType.WALK, AmapPageType.ROUTE);
                        params.setCarDirectionMode(AllorderActivity.this, 2);
                        // 启动组件
                        AmapNaviPage.getInstance().showRouteActivity(getApplicationContext(), params, null);
                        break;
                    case R.id.RIDE:
                        //起点
                        start = new Poi(null, new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)), null);
                        //终点
                        end = new Poi(null, new LatLng(lats,log), null);
                        // 组件参数配置
                        params = new AmapNaviParams(start, null, end, AmapNaviType.RIDE, AmapPageType.ROUTE);
                        params.setCarDirectionMode(AllorderActivity.this, 2);
                        // 启动组件
                        AmapNaviPage.getInstance().showRouteActivity(getApplicationContext(), params, null);
                        break;
                    case R.id.MOTORCYCLE:
                        //起点
                        start = new Poi(null, new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)), null);
                        //终点
                        end = new Poi(null, new LatLng(lats,log), null);
                        // 组件参数配置
                        params = new AmapNaviParams(start, null, end, AmapNaviType.MOTORCYCLE, AmapPageType.ROUTE);
                        params.setCarDirectionMode(AllorderActivity.this, 2);
                        // 启动组件
                        AmapNaviPage.getInstance().showRouteActivity(getApplicationContext(), params, null);
                        break;
                    case R.id.DRIVER:
                        //起点
                        start = new Poi(null, new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)), null);
                        //终点
                        end = new Poi(null, new LatLng(lats,log), null);
                        // 组件参数配置
                        params = new AmapNaviParams(start, null, end, AmapNaviType.DRIVER, AmapPageType.ROUTE);
                        params.setCarDirectionMode(AllorderActivity.this, 2);
                        // 启动组件
                        AmapNaviPage.getInstance().showRouteActivity(getApplicationContext(), params, null);
                        break;
                    default:
                        Toast.makeText(AllorderActivity.this, "请选择导航方式", Toast.LENGTH_SHORT).show();
                }

            }
        });

        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });

    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }




}
